﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataModel;
using P_PersonalDetails.Service.PersonalInfo;

namespace P_PersonalDetails
{
    public partial class PersonalInfo : UserControl
    {
        public PersonalInfo()
        {
            InitializeComponent();
            try
            {
                SRV.LoadPersonalInfoCompleted += new EventHandler<LoadPersonalInfoCompletedEventArgs>(SRV_LoadPersonalInfoCompleted);
                
            }
            catch (Exception)
            {
            }
        }

        void SRV_LoadPersonalInfoCompleted(object sender, LoadPersonalInfoCompletedEventArgs e)
        {
            this.DataContext = e.Result;
            e.Result.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Result_PropertyChanged);
        }

        void Result_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            SRV.UpdatePersonalInfoAsync((int)sender.GetPropertyValue("ID"), e.PropertyName, sender.GetPropertyValue(e.PropertyName));
        }

        public int? MemberID
        {
            get { return (int?)GetValue(MemberIDProperty); }
            set { SetValue(MemberIDProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MemberID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MemberIDProperty =
            DependencyProperty.Register("MemberID", typeof(int?), typeof(PersonalInfo), new PropertyMetadata(null, (DO, DE) =>
            {
                if (DE.NewValue != null)
                {
                    ((PersonalInfo)DO).MemberIDChanged();
                }
            }));

        void MemberIDChanged()
        {
            SRV.LoadPersonalInfoAsync(MemberID.Value);
        }


        PersonalInfoServiceClient SRV = ServiceConfig.NewPersonalInfoClient();
    }
}
